package com.hoki.zj.hrm.asserts;

import com.hoki.zj.hrm.exception.GlobalException;
import com.hoki.zj.hrm.utils.AjaxResult;
import com.hoki.zj.hrm.utils.StrUtils;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobalAssert {

    public static final String CHINA_PATTERN =
            ("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");

    public static final  String EMAIL_PATTERN =
            "^[a-zA-Z0-9_+-]+(.[a-zA-Z0-9_+-]+)*@([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\\.)+[a-zA-Z]{2,}$";

    /**
     * 字符串非空判断
     */
    public static void hasText(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new GlobalException(message);
        }
    }

    /**
     * 手机判断
     */
    public static void isValidPhoneNumber(String phoneNumber, String message) {
        Pattern p = Pattern.compile(CHINA_PATTERN, Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(phoneNumber);
        if (!matcher.matches()) {
            // 未匹配上抛出异常
            throw new GlobalException(message);
        }
    }

    /**
     * 邮箱判断
     */
    public static void isValidEmail(String email, String message) {
        Pattern p = Pattern.compile(EMAIL_PATTERN, Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(email);
        if (!matcher.matches()) {
            // 未匹配上抛出异常
            throw new GlobalException(message);
        }
    }

    /**
     * 判断字符串以外的对象为空
     */
    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new GlobalException(message);
        }
    }

    /**
     * 判断状态/类型是否相等
     */
    public static void isEquals(int target, int original, String message) {
        if (target != original) throw new GlobalException(message);
    }

    /**
     * 判断字符串
     */
    public static void isEquals(String  target, String original, String message) {
        if (!target.equalsIgnoreCase(original)) throw new GlobalException(message);
    }

    /**
     * 成功判断
     * true : 成功
     * false: 未成功
     * @param isSuccess 結果
     * @param message エラーメッセージ
     */
    public static void isSuccess(Boolean isSuccess, String message) {
        if (!isSuccess) throw new GlobalException(message);
    }

    /**
     * 未成功判断
     * true : 未成功
     * false: 成功
     */
    public static void isUnsuccess(Boolean isKilled, String message) {
        if (isKilled) throw new GlobalException(message);
    }
}
